package com.tuo51.manager.controller;

import com.tuo51.evaluate.service.UploadService;
import com.tuo51.foudation.constant.RequestConstant;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

import static com.tuo51.evaluate.service.impl.UploadServiceImpl.FILE_OP_STAFF;
import static com.tuo51.foudation.model.ErrorCode.FILE_MUST_NOT_NULL;


@RestController
@RequestMapping("/mapi/upload")
@Api(value = "/mapi/upload", tags = "OP - 上传")
public class MAPIUploadController {

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private UploadService uploadService;

    @Value("${aliyun.oss.fileServerPrefix}")
    private String IMG_SERVICE_PREFIX;

    @RequestMapping(value = "/avatar" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse uploadAvatar(@RequestHeader(RequestConstant.TICKET)String ticket, MultipartFile file) {
        if (Objects.isNull(file))
            return StatusResponse.error(FILE_MUST_NOT_NULL,"文件不能为空");

        String staffId = cache.getStaffId(ticket);

        String url = uploadService.uploadStaffFile(staffId, FILE_OP_STAFF, file);

        return StatusResponse.success(url);
    }

}
